package mysql

import (
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    gormlogger "gorm.io/gorm/logger"
    "gorm.io/gorm/schema"
    "moul.io/zapgorm2"
)








func (s *MySqlDB) Gorm() (*gorm.DB, error) {
    logger := zapgorm2.New(s.logger)
    if s.is_dev {
        logger.LogLevel = gormlogger.Info
    } else {
        logger.LogLevel = gormlogger.Warn
    }
    logger.SlowThreshold = s.dur_slow









    db, err := gorm.Open(mysql.New(mysql.Config{
        DSN: s.dsn,

        DefaultStringSize:         256,
        DisableDatetimePrecision:  true,
        DontSupportRenameIndex:    true,
        DontSupportRenameColumn:   true,
        SkipInitializeWithVersion: false,
    }), &gorm.Config{
        NamingStrategy: schema.NamingStrategy{TablePrefix: "", SingularTable: true},


        Logger: logger,
    })
    if err != nil {
        return nil, err
    }
    sqldb, err := db.DB()
    if err != nil {
        return nil, err
    }
    sqldb.SetMaxIdleConns(s.maxIdleConns)
    sqldb.SetMaxOpenConns(s.maxOpenConns)
    sqldb.SetConnMaxLifetime(s.connMaxLifeTime)

    return db, nil
}
